package com.huilian.iotbox.data.service.impl;

import com.huilian.iotbox.data.po.StatsDevice;
import com.huilian.iotbox.data.service.StatsDeviceCommonService;
import com.huilian.iotbox.data.vo.OrderVo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class StatsDeviceCommonServiceImpl implements StatsDeviceCommonService {

    // 人民币面额list
    static List<BigDecimal> denominationList = new ArrayList<>();

    static {
        // 这里的顺序不能变动哈
        denominationList.add(BigDecimal.valueOf(100).setScale(2));
        denominationList.add(BigDecimal.valueOf(50).setScale(2));
        denominationList.add(BigDecimal.valueOf(20).setScale(2));
        denominationList.add(BigDecimal.valueOf(10).setScale(2));
        denominationList.add(BigDecimal.valueOf(5).setScale(2));
        denominationList.add(BigDecimal.valueOf(1).setScale(2));
    }


    @Override
    public void statsDeviceDenomination(List<OrderVo> orderVoList, StatsDevice statsDevice) {
        if (orderVoList.size() == 0) {
            return;
        }

        for (OrderVo orderVo : orderVoList) {
            System.out.println("金额：" + orderVo.getTotalAmount());

            if (denominationList.contains(orderVo.getTotalAmount().setScale(2))) {
                // 过滤出不是面额的金额
                System.out.println("匹配面额：" + orderVo.getTotalAmount() + "元，命中次数：" + orderVo.getTotalCount());
                this.checkDenomination(statsDevice, orderVo.getTotalAmount().setScale(2), orderVo.getTotalCount());
                continue;
            }

            for (int i = 0; i < orderVo.getTotalCount(); i++) {
                // 这种面额出现了多少次就需要计算多少次
                BigDecimal totalAmount = orderVo.getTotalAmount();
                for (BigDecimal denomination : denominationList) {
                    if (totalAmount.compareTo(denomination) < 0 || BigDecimal.ZERO.compareTo(totalAmount) == 0) {
                        continue;
                    }
                    BigDecimal[] remainder = totalAmount.divideAndRemainder(denomination);

                    if (BigDecimal.ZERO.compareTo(remainder[0]) != 0) {
                        System.out.println(remainder[0] + "*" + denomination + "元");
                        totalAmount = remainder[1];
                        this.checkDenomination(statsDevice, denomination.setScale(2), remainder[0].intValue());
                    }
                }
            }
        }
    }

    @Override
    public void checkDenomination(StatsDevice statsDevice, BigDecimal totalAmount, Integer totalCount) {
        if (denominationList.get(5).compareTo(totalAmount) == 0) {
            statsDevice.setOneYuanDenomination(statsDevice.getOneYuanDenomination() + totalCount);
        } else if (denominationList.get(4).compareTo(totalAmount) == 0) {
            statsDevice.setFiveYuanDenomination(statsDevice.getFiveYuanDenomination() + totalCount);
        } else if (denominationList.get(3).compareTo(totalAmount) == 0) {
            statsDevice.setTenYuanDenomination(statsDevice.getTenYuanDenomination() + totalCount);
        } else if (denominationList.get(2).compareTo(totalAmount) == 0) {
            statsDevice.setTwentyYuanDenomination(statsDevice.getTwentyYuanDenomination() + totalCount);
        } else if (denominationList.get(1).compareTo(totalAmount) == 0) {
            statsDevice.setFiftyYuanDenomination(statsDevice.getFiftyYuanDenomination() + totalCount);
        } else if (denominationList.get(0).compareTo(totalAmount) == 0) {
            statsDevice.setOneHundredYuanDenomination(statsDevice.getOneHundredYuanDenomination() + totalCount);
        } else {
            System.out.println(statsDevice.getDeviceNo() + "面额不存在" + totalAmount);
        }
    }
}
